package pl.register.android.activities.students.builder;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import pl.register.android.R;
import pl.register.android.activities.students.logic.StudentsPresenter;
import pl.register.android.year.Year;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-12
 */
public class YearEditorDialogBuilder extends AlertDialog.Builder {

    private EditText etSubNameYear;

    private StudentsPresenter studentsPresenter;

    public YearEditorDialogBuilder(Context context, Year year, StudentsPresenter studentsPresenter) {
        super(context);
        this.studentsPresenter = studentsPresenter;

        onCreateContent(context);
        setupDialog();
        populateFields(year);
    }

    private void onCreateContent(Context ctx) {
        LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(ctx.LAYOUT_INFLATER_SERVICE);
        View dialogLayout = inflater.inflate(R.layout.dialog_input_layout, null);
        etSubNameYear = (EditText) dialogLayout.findViewById(R.id.input_edit_text);
        setView(dialogLayout);
    }

    private void setupDialog() {
        setTitle(R.string.dialog_title_change_subname_year);
        setNegativeButton(R.string.btn_name_cancel, null);
        setPositiveButton(R.string.btn_name_save, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String subName = etSubNameYear.getText().toString();
                studentsPresenter.changeSubNameYear(subName);
            }
        });
    }

    private void populateFields(Year year) {
        etSubNameYear.setText(year.getSubName());
    }

}